package com.qbccn.bbs.controller;

import com.qbccn.bbs.domain.BbsUser;
import com.qbccn.bbs.dto.ResponseResult;
import com.qbccn.bbs.service.FileService;
import com.qbccn.bbs.service.UserService;
import com.qbccn.bbs.utils.CommonUtils;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author linke
 * @date 2020/5/30 22:31
 * @description
 */
@Controller
@RequestMapping("user")
public class UserController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private UserService userService;
  @Autowired
  private FileService fileService;

  private BbsUser getUser() {
    return (BbsUser) request.getSession().getAttribute("user");
  }

  @PostMapping("sign")
  @ResponseBody
  public ResponseResult sign() {
    return userService.sign(getUser().getUsername());
  }

  @GetMapping("home")
  public String home() {
    return "user/home";
  }

  @GetMapping("index")
  public String index() {
    request.setAttribute("label", "index");
    //飞吻数
    request.setAttribute("flyKiss", userService.getFlyKissNumber(getUser().getUsername()));
    return "user/index";
  }

  @GetMapping("set")
  public String set() {
    request.setAttribute("label", "set");
    return "user/set";
  }

  @GetMapping("message")
  public String message() {
    request.setAttribute("label", "message");
    return "user/message";
  }

  @GetMapping("post")
  public String post() {
    request.setAttribute("label", "post");
    return "user/post";
  }

  /**
   * 发帖子
   */
  @GetMapping("add")
  public String add() {
    return "article/add";
  }

  @PostMapping("/updateImg")
  @ResponseBody
  public ResponseResult updateImg(@PathVariable("file") MultipartFile file) throws IOException {
    return fileService.uploadImgToAliyunOss(file, 1);
  }

  @PostMapping("updateBaseInfo")
  @ResponseBody
  public ResponseResult updateBaseInfo(@RequestParam("email") String email,
      @RequestParam("name") String name, @RequestParam("city") String city,
      @RequestParam("sign") String sign, @RequestParam("sex") Integer sex) {
    if (!CommonUtils.isEmail(email)) {
      return new ResponseResult(1, "邮箱格式有误");
    }
    if (name == null || name.length() > 12) {
      return new ResponseResult(1, "昵称格式有误：1~12个字符以内");
    }
    if (city != null && city.length() > 20) {
      return new ResponseResult(1, "城市格式有误：0~20个字符以内");
    }
    if (sex != 1 && sex != 2) {
      return new ResponseResult(1, "性别参数参数有误！");
    }
    BbsUser bbsUser = new BbsUser();
    bbsUser.setName(name);
    bbsUser.setEmail(email);
    bbsUser.setCity(city);
    bbsUser.setSign(sign);
    bbsUser.setSex(sex);
    return userService.updateBaseInfo(bbsUser);
  }

  @PostMapping("updatePassword")
  @ResponseBody
  public ResponseResult updatePassword(@RequestParam("pass") String pass,
      @RequestParam("repass") String repass, @RequestParam("nowpass") String nowpass) {
    if (nowpass == null) {
      return new ResponseResult(1, "原密码不能为空！");
    }
    String password = userService.getPassword();
    if (!new BCryptPasswordEncoder().matches(nowpass, password)) {
      return new ResponseResult(1, "原密码错误！");
    }
    if (pass == null || pass.length() > 16 || !CommonUtils.isPassword(pass)) {
      return new ResponseResult(1, "密码格式有误：由字母、数字、下划线、1~16长度组成");
    }
    if (repass == null || !repass.equals(pass)) {
      return new ResponseResult(1, "两次输入密码不一致！");
    }
    return userService.updatePassword(pass);
  }

  /**
   * 发表文章
   */
  @PostMapping("addArticle")
  @ResponseBody
  public ResponseResult addArticle() {

    return null;
  }
}
